package com.bilibili.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommenResult;
import com.bilibili.entity.Page;
import com.bilibili.entity.User;
import com.bilibili.service.UserService;
import com.bilibili.service.impl.UserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;


@WebServlet("/allUser.do")
public class AllUserServlet extends HttpServlet {
	UserService us = new UserServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    // 声明变量时直接初始化默认值
	    int pageNum = 1;
	    int pageSize = 10;
	    
	    try {
	        pageNum = Integer.parseInt(request.getParameter("pageNum"));
	    } catch (Exception e) {
	        // 已设置默认值，无需额外处理
	    }
	    
	    try {
	        pageSize = Integer.parseInt(request.getParameter("pageSize"));
	    } catch (Exception e) {
	        // 已设置默认值，无需额外处理
	    }
	    
	    Page<User> page = us.SelectAllUser(pageNum, pageSize);
	    ObjectMapper om = new ObjectMapper();
	    Map<String, Object> m = new HashMap();
	    m.put("page", page);
	    response.setContentType("application/json;charset=utf-8");
	    response.getWriter().write(om.writeValueAsString(m));
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
